package main.KBProcess;

/*
	ALAR: 		Match_Table Class
	Programmer:	John Talburt
	Date:		February 9, 2009
	Filename:	Match_Table.java
	Purpose:	An external class used to load a list of items and
				determine if a search items matches an item in the list
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class UTEES_MatchTable implements Serializable, Cloneable
{
	ArrayList<String> table = new ArrayList<String>();
	BufferedReader dataIn;
	File tableFile;
	int maxLen;
	int minLen;
	int index;

	public UTEES_MatchTable(File tableName)
	{
		this.tableFile = tableName;
		try
		{
                    dataIn = new BufferedReader(new FileReader(tableFile));
		}
		catch(IOException e)
		{
                    System.out.println("Match_Table: Can't Open File: "+tableFile);
                    System.out.println(e);
                    System.exit(0);
		}

		String word;
		int count = 0;
		try
		{
                    minLen = 99;
                    maxLen = 0;
                    word=dataIn.readLine().toLowerCase();
//                    if(word != null)
//                        {
//                            word = word.replaceAll("[^ A-Za-z0-9!\"#$%&'()*+,./:;<=>?@\\_`{|}~-]", "");
//                        }
                    while(word != null)
                    {
                        int len = word.length();
                        if(len>maxLen) maxLen = len;
                        if(len<minLen) minLen = len;
                        int insPt = Arrays.binarySearch(table.toArray(), word);
                        if(insPt<0)
                        {
                                insPt = -insPt-1;
                                table.add(insPt, word);
                        }
                        word=dataIn.readLine();
                        word = word != null ? word.toLowerCase():word;

//                        if(word != null)
//                        {
//                            word = word.replaceAll("[^ A-Za-z0-9!\"#$%&'()*+,./:;<=>?@\\_`{|}~-]", "");
//                        }

                        count++;
                    }
		}
		catch(IOException e)
		{
                    System.out.println("Problem in readLine");
		}

	}

	public boolean findMatch(String target)
	{
		index = Arrays.binarySearch(table.toArray(), target);
		if(index<0)
		{
                    return false;
		}
		else
		{
                    return true;
		}

	}

	public int getMatchIndex()
	{
		return index;
	}

    public String getFileName() {
        return tableFile.getName();
    }

	public String getEntryAt(int intVal)
	{
		String retVal = "";
		if(intVal>=0 && intVal<table.size())
		{
                    retVal = table.get(intVal);
		}
		return retVal;
	}

	public void dumpList()
	{
		System.out.println("Contents of "+tableFile);
		for (int j=0;j<table.size();j++)
                    System.out.println("word "+(j+1)+" is "+table.get(j));
	}

	public void confirmLoad()
	{
            System.out.println("Table >"+tableFile+"< loaded with >"+table.size()+"< entries");
            System.out.println("Max Len: "+maxLen+" Min Len: "+minLen);
	}

	public int getTableSize()
	{
            return table.size();
	}

}